package file;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.List;

public class MonitorHandler {
	
	private GraphicsDevice[] graphicsDevices;
	private GraphicsDevice defaultGraphicsDevice;
	private DisplayMode defaultDisplayMode;
	
	public MonitorHandler() {

		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		this.graphicsDevices = ge.getScreenDevices();
		this.defaultGraphicsDevice = ge.getDefaultScreenDevice();
		this.defaultDisplayMode = defaultGraphicsDevice.getDisplayMode();
	}

	public Dimension getResolution(){
		return new Dimension(defaultDisplayMode.getWidth(),defaultDisplayMode.getHeight());
	}
	
	public Integer[] getMonitors() {

		ArrayList<Integer> monitorList = new ArrayList<Integer>();
		for (int i = 0; i < graphicsDevices.length; i++) {
			monitorList.add(i);
		}

		return monitorList.toArray(new Integer[0]);
	}

	/**
	 * Fetches all supported monitor resolutions of the monitor supplied with
	 * the parameter and returns all resolutions that have the native screen
	 * ratio in a String[] where each String looks like this: 1024x768@60Hz
	 * 
	 * @param monitorNumber
	 *            - the number of the monitor which the resolutions should be
	 *            fetched from
	 * @return a String Array holding supported monitor resolutions in this
	 *         format: 1024x768@60Hz
	 */
	public String[] getMonitorResolutions(int monitorNumber) {

		DisplayMode[] modes = graphicsDevices[monitorNumber].getDisplayModes();

		/*float screenRatio = (float) defaultDisplayMode.getWidth() / (float) defaultDisplayMode.getHeight();*/

		List<String> resolutionList = new ArrayList<String>();

		for (int i = 0; i < modes.length; i++) {
			if (modes[i].getBitDepth() != 32) {
				continue;
			}

			int screenWidth = modes[i].getWidth();
			int screenHeigth = modes[i].getHeight();
			int refresh = modes[i].getRefreshRate();

			// Filter to only show native ratio Resolutions
			// 16:10
			if (/*screenRatio > 1.59 && screenRatio < 1.61 &&*/ (float) screenWidth / (float) screenHeigth > 1.59
					&& (float) screenWidth / (float) screenHeigth < 1.61) {
				resolutionList.add(screenWidth + "x" + screenHeigth + "@" + refresh + "Hz 16:10");
			}
			// 16:9
			else if (/*screenRatio > 1.77 && screenRatio < 1.78 &&*/ (float) screenWidth / (float) screenHeigth > 1.77
					&& (float) screenWidth / (float) screenHeigth < 1.78) {
				resolutionList.add(screenWidth + "x" + screenHeigth + "@" + refresh + "Hz 16:9");
			}
			// 5:4
			else if (/*screenRatio > 1.24 && screenRatio < 1.26 &&*/ (float) screenWidth / (float) screenHeigth > 1.24
					&& (float) screenWidth / (float) screenHeigth < 1.26) {
				resolutionList.add(screenWidth + "x" + screenHeigth + "@" + refresh + "Hz 5:4");
			}
			// 4:3
			else if (/*screenRatio > 1.33 && screenRatio < 1.34 &&*/ (float) screenWidth / (float) screenHeigth > 1.33
					&& (float) screenWidth / (float) screenHeigth < 1.34) {
				resolutionList.add(screenWidth + "x" + screenHeigth + "@" + refresh + "Hz 4:3");
			}
		}

		return resolutionList.toArray(new String[0]);
	}

}
